package com.yonyou.iuap.base.aop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.base.sl.ParseJsontoPsw;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.pub.ResultDto;
public class ShellKmMethodInterceptor implements IShellKmMethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		
		ResultDto rsDto = new ResultDto();
		//如果是文件上传，文件下载那么跳过签名的验证
		if("uploadFile".equals(invocation.getMethod().getName())
				||invocation.getMethod().getName().contains("downloadFile")
				||invocation.getMethod().getName().contains("picdownload")){
			Object result = invocation.proceed();
			return result;
		}
		Object[] params = invocation.getArguments();
		Map parMap = null;
		HttpServletRequest request = null;
		HttpServletResponse response = null;
		if(StringUtils.isEmpty(params)){
			rsDto.setStatus(-2);
			rsDto.setMessage("未获取到传入参数信息!");
			return JsonUtils.toJsonStr(rsDto);
		}
		for(Object obj : params){
			if(obj instanceof Map){
				parMap = (Map) obj;
			}else if(obj instanceof HttpServletRequest){
				request = (HttpServletRequest) obj;
			}else if(obj instanceof HttpServletResponse ){
				response = (HttpServletResponse) obj;
			}
			
		}
		//String str = getParamsByRequest(request);
		if(parMap==null){
			rsDto.setStatus(-2);
			rsDto.setMessage("未获取到签名Map参数信息!");
			return JsonUtils.toJsonStr(rsDto);
		}
		ParseJsontoPsw psw = new ParseJsontoPsw();
		//验签
		boolean is_check = psw.parseJson(JsonUtils.toJsonStr(parMap));
		if(is_check  ){
			if(parMap!=null){
				if(request!=null && parMap.get("data")!=null){
					//验签成功;调整request参数(由于之前ctrl的参数格式都已经固定，增加验签调整了传入的map的格式，所以此处进行调整)
					//获取参数中data数据
					Map dataMap = (Map) parMap.get("data");
					if(dataMap != null && dataMap.size()>0){
						Object[] keys = dataMap.keySet().toArray();
						for(Object obj : keys){
							parMap.put(obj.toString(),  dataMap.get(obj.toString()));
						}
						
					}
				}		
			}
				
		}else{
			//验签失败
			rsDto.setStatus(-2);
			rsDto.setMessage("签名校验失败!");
			return JsonUtils.toJsonStr(rsDto);
		}
		Object result = invocation.proceed();
		return result;
	}


	private String getParamsByRequest(HttpServletRequest request){
		//获取post参数 
		StringBuffer sb = new StringBuffer() ; 
		InputStream is;
		try {
			is = request.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);   
			BufferedReader br = new BufferedReader(isr); 
			String s = "" ; 
			while((s=br.readLine())!=null){ 
				sb.append(s) ; 
			} 
			String str =sb.toString(); 
			//防止用get传递参数 
			if(str.equals("")){ 
				if(request.getQueryString() != null) { 
					str = request.getRequestURL()+"?"+request.getQueryString(); 
				} else { 
					str = request.getRequestURL().toString(); 
				} 
			}
			return str;
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		} 
	}

}
